package com.dcyao.musicserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dcyao.musicserve.common.R;
import com.dcyao.musicserve.domain.StaffList;
import com.dcyao.musicserve.domain.dto.StaffDTO;
import com.dcyao.musicserve.domain.dto.StaffListDTO;
import com.dcyao.musicserve.mapper.StaffListMapper;
import com.dcyao.musicserve.mapper.StaffMapper;
import com.dcyao.musicserve.service.StaffListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author admin
* @description 针对表【staff_list(乐谱分类表)】的数据库操作Service实现
*/
@Service
public class StaffListServiceImpl extends ServiceImpl<StaffListMapper, StaffList>
implements StaffListService{
    @Autowired
    private StaffListMapper staffListMapper;
    @Autowired
    private StaffMapper staffMapper;

    /**
     * 分页查找
     * @param pageNum
     * @param pageSize
     * @param style
     * @return
     */
    @Override
    public R findPage(Integer pageNum, Integer pageSize, String style) {
        LambdaQueryWrapper<StaffList> wrapper = Wrappers.<StaffList>lambdaQuery();
        if (StringUtils.isNotBlank(style)) {
            wrapper.like(StaffList::getStyle, style);
        }
        Page<StaffList> userPage = staffListMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return R.success("查询成功", userPage);
    }

    /**
     * 新增
     * @param staffList
     * @return
     */
    @Override
    public R addStaffList(StaffList staffList) {
        int insert = staffListMapper.insert(staffList);
        if (insert > 0) {
            return R.success("新增成功");
        } else {
            return R.error(400, "新增失败");
        }
    }

    /**
     * 编辑
     * @param updateRequest
     * @return
     */
    @Override
    public R updateStaffList(StaffListDTO updateRequest) {
        StaffList staffList = new StaffList();
        BeanUtils.copyProperties(updateRequest, staffList);
        int i = staffListMapper.updateById(staffList);
        if (i > 0) {
            return R.success("修改成功");
        } else {
            return R.error(400, "修改失败");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public R deleteStaffList(Integer id) {
        if (staffListMapper.deleteById(id) > 0) {
            return R.success("删除成功");
        } else {
            return R.error(400, "删除失败");
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public R deleteBathIds(List<Integer> ids) {
        System.out.println("ids = " + ids);
        int i = staffListMapper.deleteBatchIds(ids);
        if (i < 0) {
            return R.error(400, "批量删除失败");
        }
        return R.success("批量删除成功");
    }

    /**
     * 乐谱集总数
     * @return
     */
    @Override
    public int staffListCount() {
        int a = staffListMapper.staffListCount();
        return a;
    }

    /**
     * 查询所有乐谱集
     * @return
     */
    @Override
    public R allStaffList() {
        List list = new ArrayList();
        List<StaffList> staffLists = staffListMapper.selectList(null);
        for (StaffList staffList : staffLists) {
            List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffList.getId());
            if (staffDTOS.size() == 0) {
                continue;
            }
            list.add(staffList);
        }
        return R.success(null, list);
    }

    /**
     * 根据乐谱名模糊查询
     * @param name
     * @return
     */
    @Override
    public R likeStaffListName(String name) {
        LambdaQueryWrapper<StaffList> wrapper = Wrappers.<StaffList>lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like(StaffList::getTitle, name);
        }
        List<StaffList> staffList = staffListMapper.selectList(wrapper);
        if (staffList == null) {
            return R.error(400, "查询失败");
        }
        return R.success("查询成功", staffList);
    }

    /**
     * 根据风格查询对应的乐谱集（乐谱集有数据）
     * @param style
     * @return
     */
    @Override
    public R StaffListStyle(String style) {
        List list = new ArrayList();
        LambdaQueryWrapper<StaffList> wrapper = Wrappers.<StaffList>lambdaQuery();
        wrapper.eq(StaffList::getStyle, style);
        List<StaffList> staffLists = staffListMapper.selectList(wrapper);
        for (StaffList staffList : staffLists) {
            List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffList.getId());
            if (staffDTOS.size() == 0) {
                continue;
            }
            list.add(staffList);
        }
        if (list == null) {
            return R.error(400, "暂无数据显示");
        }
        return R.success("查询成功", list);
    }

    /**
     * 根据风格查询对应的乐谱集
     * @param style
     * @return
     */
    @Override
    public R StaffListStyleAll(String style) {
        LambdaQueryWrapper<StaffList> wrapper = Wrappers.<StaffList>lambdaQuery();
        wrapper.eq(StaffList::getStyle, style);
        List<StaffList> staffLists = staffListMapper.selectList(wrapper);
        System.out.println("乐谱集数据============" );
        System.out.println("staffLists = " + staffLists);
        if (staffLists == null) {
            return R.error(400, "暂无数据显示");
        }
        return R.success("查询成功", staffLists);
    }

    /**
     * 根据风格统计乐谱集里面乐谱的总数
     * @param
     * @return
     */
    @Override
    public R StyleStaff() {
        int size11 = 0;
        int size22 = 0;
        int size33 = 0;
        int size44 = 0;
        Map<String,Object> map = new HashMap();
        List<StaffList> staffLists = staffListMapper.selectList(null);
        for (StaffList staffList : staffLists) {
            if (staffList.getStyle().equals("华语")) {
                List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffList.getId());
                int size = staffDTOS.size();
                size11 += size;
            }
            if (staffList.getStyle().equals("欧美")) {
                List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffList.getId());
                int size2 = staffDTOS.size();
                size22 += size2;
            }
            if (staffList.getStyle().equals("名师大家")) {
                List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffList.getId());
                int size3 = staffDTOS.size();
                size33 += size3;
            }
            if (staffList.getStyle().equals("轻音乐")) {
                List<StaffDTO> staffDTOS = staffMapper.selectStaffs(staffList.getId());
                int size4 = staffDTOS.size();
                size44 += size4;
            }
        }
        map.put("华语",size11);
        map.put("欧美",size22);
        map.put("名师大家",size33);
        map.put("轻音乐",size44);
        return R.success("查询成功", map);
    }

    /**
     * 根据风格查询对应的乐谱集
     *
     * @return
     */
    @Override
    public R StaffStyle() {
        List staffList = staffListMapper.StaffStyle();
        if (staffList == null) {
            return R.error(400, "查询失败");
        }
        return R.success("查询成功", staffList);
    }



}
